<template>
    <div id="app" class="container">
        <el-form label-position="top" :model="user" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="login-box">
            <el-form-item label="用户名" prop="username">
                <el-input type="text" v-model="user.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="user.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="userLogin('ruleForm')">登录</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "Login",
    data() {
        let validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                callback();
            }
        };
        let validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入用户名'));
            } else {
                callback();
            }
        };
        return {
            user: {
                password: '',
                username: ''
            },
            rules: {
                password: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                username: [
                    { validator: validatePass2, trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        userLogin(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$axios.post("http://localhost:9091/admin/manager/login", {
                        username: this.user.username,
                        password: this.user.password
                    })
                        .then(data => {
                            console.log(data)
                            if (data.data.code === 200) {
                                this.$message({
                                    type:"success",
                                    message: data.data.msg
                                });
                                sessionStorage.setItem("token", data.data.token);
                                this.$router.push({
                                    name: "TeacherList"
                                })
                            } else {
                                this.$message.error(data.data.msg)
                            }
                        })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style scoped>
    #app {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
        background: url("../assets/images/logback.png");
        background-size: 1100px;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .login-box {
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
        width: 300px;
        padding: 20px;
        line-height: 20px;
    }

    .login-box >>> .el-form-item__label {
        line-height: 20px;
        font-weight: bold;
        font-size: 1.2em;
    }

    body:first-child {
        background: red;
    }

</style>
